package middleware

import (
    systemConfig "gin-worked/app/system/config"
    "gin-worked/config"
    "github.com/gin-gonic/gin"
    "net/http"
)

// SystemMiddleware 登录验证
func SystemMiddleware(c *gin.Context) {
    token := c.Request.Header.Get("accept-token")

    if token == "" {
        Ret(c, 10000, "token不存在", "")
        return
    }

    userToken := config.Rdb.Get(config.Rctx, token)

    if userToken.Val() == "" {
        //验证缓存里面的token是否参照
        Ret(c, 10001, "token不存在,请重新登录", nil)
        return
    }

    system, err := systemConfig.TokenDecrypt(token)

    if err != nil {
        //验证缓存里面的token是否参照
        Ret(c, 0, err.Error(), nil)
        return
    }

    c.Set("uid", system.Id)
    c.Next()
    //return
}

func Ret(c *gin.Context, code int, msg interface{}, data interface{}) {
    c.JSON(http.StatusBadRequest, gin.H{"Code": code, "Msg": msg, "Data": data})
    c.Abort()
    return
}
